package cn.team11.sdsPackager;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.aliyun.alink.sdk.jsbridge.BoneCallback;
import com.aliyun.alink.sdk.jsbridge.IJSBridge;
import com.aliyun.alink.sdk.jsbridge.methodexport.BaseBonePlugin;
import com.aliyun.alink.sdk.jsbridge.methodexport.MethodExported;
import com.uuzuche.lib_zxing.activity.CodeUtils;
import com.uuzuche.lib_zxing.activity.ZXingLibrary;

import org.json.JSONException;
import org.json.JSONObject;

import cn.team11.sds.dayi.debugpage.CustomCaptureActivity;
import cn.team11.util.T11Log;


/**
 * Created by liyuan on 2017/9/27.
 */

public class T11ScanQrcodePlugin extends BaseBonePlugin {

    public static final String API_NAME = "T11ScanQrcodePlugin";
    private BoneCallback qrCodeCallback = null;
    private static final int REQUEST_CODE_QR_CODE = 0x1000;
    public static final int REQUEST_CODE=1;
    public static final int REQUEST_IMAGE=2;
    public static final int REQUEST_CUSTOM=3;


    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (Activity.RESULT_OK == resultCode
                && REQUEST_CODE_QR_CODE == requestCode) {

            Bundle bundle = data.getExtras();

            if (bundle == null) {
                return;
            }

            if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) {
                if(qrCodeCallback !=null){
                    qrCodeCallback.failed("110000","二维码扫描失败");
                }
//                Toast.makeText(context, "解析二维码失败", Toast.LENGTH_LONG).show();
                return;
            }

            if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) {
                String result = bundle.getString(CodeUtils.RESULT_STRING);
                try {
                    JSONObject obj = new JSONObject();
                    obj.put("qrCode", result);
                    qrCodeCallback.success(obj);
                    T11Log.d( "qrCodeCallback.success");
                } catch (JSONException e) {
                    e.printStackTrace();
                    T11Log.d( "qrCodeCallback.failed");
                    if (qrCodeCallback != null) {
                        qrCodeCallback.failed("110000", e.toString());
                    }
                }
            }
        }
    }

    public void onInitialize(Context var1, IJSBridge var2) {
        super.onInitialize(var1, var2);
        ZXingLibrary.initDisplayOpinion(var1);
    }


    @MethodExported
    public void startScan(String arg1,BoneCallback boneCallback) {
        qrCodeCallback = boneCallback;
        try {
//            Intent intent = new Intent(jsBridge.getCurrentActivity(), CaptureActivity.class);

            Intent intent = new Intent(jsBridge.getCurrentActivity(), CustomCaptureActivity.class);
//            startActivityForResult(intent, REQUEST_CUSTOM);
            jsBridge.getCurrentActivity().startActivityForResult(intent, REQUEST_CODE_QR_CODE);
//            jsBridge.getCurrentActivity().startActivityForResult(intent, REQUEST_CODE_QR_CODE);
        } catch (Exception e) {
            T11Log.e( "open data failed", e);
        }
    }
}